package com.zzj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzj.base.Result;
import com.zzj.entity.Admin;
import com.zzj.entity.User;
import com.zzj.model.dto.QueryListDTO;
import com.zzj.service.IAdminService;
import com.zzj.service.IUserService;
import com.zzj.utils.QueryUtil;
import jakarta.annotation.Resource;
import org.apache.http.HttpHeaders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zzj
 * @since 2023-02-18
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private IAdminService adminService;
    @Resource
    private IUserService userService;

    @Transactional
    @PostMapping("formalList")
    public Result formalList(@RequestBody @Validated QueryListDTO in) {

        QueryWrapper<Admin> queryWrapper = QueryUtil.paramsWrapper(in.getAndParams(), in.getOrParams(), in.getOrderParams());
        queryWrapper.eq("status", "1");
        Page<Admin> adminPage = new Page<>(in.getPageNo(), in.getPageSize());
        IPage<Admin> adminIPage = adminService.getBaseMapper().selectPage(adminPage, queryWrapper);
        return Result.success(adminIPage);
    }
    @Transactional
    @PostMapping("infoById")
    public Result infoById(@RequestParam String id) {
        Admin admin = adminService.getAdminById(Integer.parseInt(id));
        return Result.success(admin);
    }
    @Transactional
    @PostMapping("infoByUsername")
    public Result infoByUsername(@RequestParam String username) {
        Admin admin = adminService.getAdminByUsername(username);
        return Result.success(admin);
    }

    /**
     * 管理员权限
     *
     * @param token
     * @return
     */
    @Transactional
    @PostMapping("agreeByUsername")
    public Result agreeByUsername(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String token,
                                  @RequestParam(value = "targetUsername") String targetUsername) {
        if (!userService.isSuperAdmin(token)) {
            return Result.error("无权操作");
        }
        User userByUsername = userService.findUserByUsername(targetUsername);
        if(userByUsername==null){
            return Result.error("用户不存在");
        }
        userByUsername.setStatus(5);
        userService.updateById(userByUsername);
        Admin admin = new Admin();
        admin.setUsername(userByUsername.getUsername());
        admin.setAvatar(userByUsername.getAvatar());
        admin.setStatus(1);
        adminService.save(admin);
        return Result.success("已同意");
    }

    /**
     * 管理员权限
     *
     * @param token
     * @return
     */
    @Transactional
    @PostMapping("disAgreeByUsername")
    public Result disAgreeByUsername(@RequestHeader(value = HttpHeaders.AUTHORIZATION) String token,
                                     @RequestParam(value = "targetUsername") String targetUsername) {
        if (!userService.isSuperAdmin(token)) {
            return Result.error("无权操作");
        }
        userService.lambdaUpdate()
                .eq(User::getUsername, targetUsername)
                .set(User::getStatus, 555).update();
        return Result.success("已拒绝");
    }
}
